/*** Class Name:  OORTelesalesLeadBatchJob
 * Author: Accenture
 * Date: 19-April-2012
 * Requirement/Project Name: OORTelesales
 * Requirement Description: Daily Job to check if any lead  TemporaryOwner__c is enabled.
 **/

global class OORTelesalesLeadBatchJob implements Database.Batchable<sObject>,Schedulable
{
    private final String  query;
    private final ID campaignLeadQueue; 

    /** 
     *  Constructor
     *
     */      
    global OORTelesalesLeadBatchJob ()
    {
       final ID localAdvertiserRecordTypeID = RecordTypeHelper.GetRecordTypeIdByDevName('Lead', OORTelesalesTextConstant.LOCAL_ADVERTISER_LEAD_REC_TYPE);         
       campaignLeadQueue =  OORTelesalesUtility.getQueueID(OORTelesalesTextConstant.OOR_TELE_CAMPAIGN_LEAD_QUEUE); 
       // Get the lead if TemporaryOwner__c checkbox is enabled.      
       query = 'select ownerID,TemporaryOwner__c  from Lead where TemporaryOwner__c = true and RecordTypeId=\''+ localAdvertiserRecordTypeID +'\'';
    }
    
    /**
     * The executeBatch method is called for each chunk of records returned from start.   
     *
     */
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
          return Database.getQueryLocator(query);
    }
    
    /**
     * The execute method is called for each batch of records passed to the method. 
     *
     */
    global  void execute(Database.BatchableContext BC, List<sObject> leadScopeList )
    {       
        Lead leadObj = null;
                
        for(Sobject obj : leadScopeList )
        {
            leadObj = (Lead)obj;
            leadObj.TemporaryOwner__c = false;
            leadObj.ownerID = campaignLeadQueue;
            leadObj.LeadVisitedCounter__c =  leadObj.LeadVisitedCounter__c == null ? 1 : leadObj.LeadVisitedCounter__c +1;  
        }
        
        try
        {
            // update lead if any elements are present.
            if(leadScopeList.size()>0)
            {
                update leadScopeList ;
            }
         } 
         catch(Exception e)
         {
             GeneralConstants.mailAnyExceptionToMe('OORTelesalesLeadBatchJob ','execute',e);
         }
    }
    
   /**
    * The finish method is called after all batches are processed. Use this method to send confirmation emails or execute
    * post-processing operations.
    *
    */
    global void finish(Database.BatchableContext BC)
    {
    }
     
    /**
     *  Implementing scheduler Execute method.
     *
     */
    global void execute(SchedulableContext sc) 
    {
        OORTelesalesLeadBatchJob  leadBatchJob  = new  OORTelesalesLeadBatchJob();
        ID batchprocessid = Database.executeBatch(leadBatchJob);      
    }  
    
    /**
     * Test method.
     *
     */
     @isTest(SeeAllData=true)
     public static void testLeadBatch () 
     {
        test.starttest();
        ID localAdvertiserRecordTypeID = RecordTypeHelper.GetRecordTypeIdByDevName('Lead', OORTelesalesTextConstant.LOCAL_ADVERTISER_LEAD_REC_TYPE);   
        Lead  le  = new Lead(FirstName='testFirstName',LastName = 'ash', TemporaryOwner__c= true,company='ABc',Lead_Status_ReadOnly__c = 'New' ,Status='New',RecordTypeId= localAdvertiserRecordTypeID );
        insert le;
        OORTelesalesLeadBatchJob  leadBatchJob  = new  OORTelesalesLeadBatchJob();
        ID batchprocessid = Database.executeBatch(leadBatchJob  );
                               
        String sch = '0 0 23 * * ?';
        system.schedule('OORTelesalesLead', sch , leadBatchJob  ); 
        Test.stopTest();
     }         
}